package com.tianqu.tms.entity;

import java.util.ArrayList;
import java.util.List;

import com.tianqu.tms.core.base.TmsEntity;
import com.tianqu.tms.core.exception.TmsDaoException;
import com.tianqu.tms.core.exception.TmsException;
import com.tianqu.tms.dao.TmsDao;
import com.tianqu.tms.dao.UserRoleDao;
import com.tianqu.tms.dto.UserRoleDto;
/**
 * 用户角色表
 * 
 * @author tianshifu
 *
 */
public class UserRole extends TmsEntity {

	/**
	 * 
	 */
	private static final long serialVersionUID = 2267411555460484726L;

	private int ucId;// 用户组ID

	private int role;// 角色ID

	private String roleName;// 角色名称
	
	private int used;//是否在用 1:是 2：否

	private String radio;//radio的名字
	
	private int checked;//是否被选择 1：是2：否
	
	private int num;//列表序号
	
	private int cssFlag;//样式
	
	private String ucName;//用户组名称
	
	private int roleStatus;//1.默认2.隐藏
	
	private String funcIds;
	
	private int trFlag;
	
	/**
	 * 实例化UserRole表
	 * 
	 * @return
	 */
	private static UserRole instance;

	List<UserRole> codes = null;

	/**
	 * 加载UserRole
	 * 
	 * @return
	 * @throws TmsDaoException
	 */
	public static UserRole getInstance() throws TmsDaoException {
		instance = new UserRole();
		return instance;
	}
	
	
	/**
	 * 载入用户Class中所有role和正在使用的
	 * @return
	 * @throws TmsDaoException 
	 */
	@SuppressWarnings("unchecked")
	public List getUsedRole(List<UserAuth> authList,int ucId) throws TmsDaoException{
		List usedList = new ArrayList();
		
		List<UserRole> userList  = getRoleinfoByUcId(ucId,2);
		int k=1;
		
			for(UserRole userRole : userList){
				UserRole uRole = new UserRole();
				if(k % 2 == 0){
					uRole.setTrFlag(2);
				}else{
					uRole.setTrFlag(1);
				}
				int used = 2;
				uRole.setRoleName(userRole.getRoleName());
				if(authList != null && authList.size() > 0){
					used = getRoleUsed(userRole.getRole(),authList);
				}				
				uRole.setUsed(used);
				uRole.setRadio("radio"+k);
				uRole.setRole(userRole.getRole());
				usedList.add(uRole);
				k++;
			}
		
		
		return usedList;
	}
	
	/**
	 * 匹配正在使用中的role
	 * @param role
	 * @param authList
	 * @return
	 */
	@SuppressWarnings("unchecked")
	private int getRoleUsed(int role,List authList){
		int used=2;
		for(int i=0;i<authList.size();i++){
			UserAuth userAuth = UserAuth.getInstance();
			userAuth = (UserAuth)authList.get(i);
			if(userAuth.getrId() == role){
				return used = 1;
			}
		}
		return used;
	}
	
	
	/**
	 * 根据用户组ID查询角色ID
	 * 
	 * @return
	 * @throws TmsDaoException 
	 */
		
	public List<UserRole> getRoleinfoByUcId(int ucId,int type) throws TmsDaoException
	{
		UserRoleDao dao = (UserRoleDao) TmsDao.getInstance(TmsDao.TMS_USER_ROLE);
		
		List<UserRoleDto> dtoList  = dao.getRoleInfoByUcId(ucId);
		
		List<UserRole> list = new ArrayList<UserRole>();
		
		UserRole entity = null;
		
		if(dtoList.size()>0)
		{
			for(int i=0;i<dtoList.size();i++)
			{
				UserRoleDto dto = dtoList.get(i);
				entity = new UserRole();
				entity.setRole(dto.getRole());
				entity.setRoleName(dto.getRoleName());
				if(type == 1){
					if(dto.getRoleStatus() == 1){
						list.add(entity);
					}
				}else{
					list.add(entity);
				}
				
				
			}
		}
		
		return list;
	}
	
	/**
	 * 载入所有role
	 * @return
	 * @throws TmsDaoException 
	 */
	@SuppressWarnings("unchecked")
	public List getAllRole() throws TmsDaoException{
		List allRList = new ArrayList();
		UserRoleDao dao = (UserRoleDao) TmsDao.getInstance(TmsDao.TMS_USER_ROLE);
		List<UserRoleDto> userRoleList = new ArrayList<UserRoleDto>();
		userRoleList = dao.getAllRole();
		UserClass userClss = UserClass.getInstance();
		
		int i = 1;
		
		for(UserRoleDto dto : userRoleList){
			UserRole userRole = new UserRole();
			userRole.setRole(dto.getRole());
			userRole.setRoleName(dto.getRoleName());
			userRole.setUcId(dto.getUcId());
			String userClassName = userClss.getUserClassNameByUserClass(dto.getUcId());
			userRole.setUcName(userClassName);
			userRole.setRoleStatus(dto.getRoleStatus());
			userRole.setNum(i);
			if(i%2 == 0){
				userRole.setCssFlag(1);
			}else{
				userRole.setCssFlag(2);
			}
			i++;
			allRList.add(userRole);
		}
		return allRList;
	}
	
	/**
	 * 用户角色添加
	 * @param userRole
	 * @throws TmsDaoException 
	 */
	public void insertRole(UserRole userRole) throws TmsDaoException{
		UserRoleDto dto = new UserRoleDto();
		
		dto.setRole(userRole.getRole());
		dto.setRoleName(userRole.getRoleName());
		dto.setRoleStatus(userRole.getRoleStatus());
		dto.setUcId(userRole.getUcId());
		dto.setFuncIds(userRole.getFuncIds());
		UserRoleDao dao = (UserRoleDao) TmsDao.getInstance(TmsDao.TMS_USER_ROLE);
		dao.insert(dto);
		
	}
	
	
	
	
	
	
	/**
	 * 取得要添加的ucId
	 * @return
	 * @throws TmsDaoException 
	 */
	public int findRole(int ucId) throws TmsDaoException{
		int role =1;
		UserRoleDao dao = (UserRoleDao) TmsDao.getInstance(TmsDao.TMS_USER_ROLE);
		
		role = dao.getRole(ucId);
		return role+10;
		
	}
	
	/**
	 * 根据role取得信息
	 * @param role
	 * @return
	 * @throws TmsDaoException 
	 */
	public UserRole findRoleByRole(int role) throws TmsDaoException{
		UserRole userRole = new UserRole();
		UserRoleDao dao = (UserRoleDao) TmsDao.getInstance(TmsDao.TMS_USER_ROLE);
		UserRoleDto roleDto = dao.findRoleByrole(role);
		userRole.setUcId(roleDto.getUcId());
		userRole.setRole(roleDto.getRole());
		userRole.setRoleName(roleDto.getRoleName());
		userRole.setRoleStatus(roleDto.getRoleStatus());
		userRole.setFuncIds(roleDto.getFuncIds()); 
		return userRole;
	}
	
	
	/**
	 * 根据role删除
	 * @param role
	 * @throws TmsDaoException 
	 */
	public void deleteRole(int role) throws TmsDaoException{
		UserRoleDao dao = (UserRoleDao) TmsDao.getInstance(TmsDao.TMS_USER_ROLE);
		dao.deleteRole(role);
	}
	
	
	/**
	 * 判断是否有相同的角色名称
	 * @return
	 * @throws TmsDaoException
	 */
	public int findRoleByRoleName(String roleName)throws TmsDaoException{
		int flg = 1;
		UserRoleDao dao = (UserRoleDao) TmsDao.getInstance(TmsDao.TMS_USER_ROLE);
		flg = dao.getRoleByName(roleName);
		return flg;
	}
	public int getUcId() {
		return ucId;
	}

	public void setUcId(int ucId) {
		this.ucId = ucId;
	}

	public int getRole() {
		return role;
	}

	public void setRole(int role) {
		this.role = role;
	}

	public String getRoleName() {
		return roleName;
	}

	public void setRoleName(String roleName) {
		this.roleName = roleName;
	}

	
	
	public int getUsed() {
		return used;
	}


	public void setUsed(int used) {
		this.used = used;
	}


	public String getRadio() {
		return radio;
	}


	public void setRadio(String radio) {
		this.radio = radio;
	}


	public int getChecked() {
		return checked;
	}


	public void setChecked(int checked) {
		this.checked = checked;
	}


	


	@Override
	public void save() throws TmsDaoException, TmsException {
		// TODO Auto-generated method stub

	}


	


	public int getNum() {
		return num;
	}


	public void setNum(int num) {
		this.num = num;
	}


	public int getCssFlag() {
		return cssFlag;
	}


	public void setCssFlag(int cssFlag) {
		this.cssFlag = cssFlag;
	}


	public String getUcName() {
		return ucName;
	}


	public void setUcName(String ucName) {
		this.ucName = ucName;
	}


	public int getRoleStatus() {
		return roleStatus;
	}


	public void setRoleStatus(int roleStatus) {
		this.roleStatus = roleStatus;
	}


	public String getFuncIds() {
		return funcIds;
	}


	public void setFuncIds(String funcIds) {
		this.funcIds = funcIds;
	}


	public int getTrFlag() {
		return trFlag;
	}


	public void setTrFlag(int trFlag) {
		this.trFlag = trFlag;
	}




	
}
